package cn.yangliu.nacos.oauth2.service.impl;

import cn.yangliu.jpa.base.service.impl.JpaServiceImpl;
import cn.yangliu.nacos.oauth2.entity.Oauth2Client;
import cn.yangliu.nacos.oauth2.repository.Oauth2ClientRepository;
import cn.yangliu.nacos.oauth2.service.Oauth2ClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * The interface Oauth 2 client service.
 *
 * @author 问道于盲
 * @date 2019 -03-26 01:20
 */
@Service
@Slf4j
@Transactional(rollbackFor = Throwable.class)
@Primary //要设置primary 否则无限递归
public class Oauth2ClientServiceImpl
        extends JpaServiceImpl<Oauth2Client, Long, Oauth2ClientRepository>
        implements Oauth2ClientService {

    /**
     * current class instance's member.
     * The Password encoder.
     */
    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * Init.
     */
    @PostConstruct
    public void init(){
        try{
            Oauth2Client client = new Oauth2Client();
            client.setClientSecret(passwordEncoder.encode("admin"));
            client.setClientId("admin");

            client.setScopes("all");
            client.setAccessTokenValiditySeconds(1000000);
            client.setRefreshTokenValiditySeconds(1000000);
            client.setGrantTypes("password,authorization_code,implicit,client_credentials,refresh_token");
            client.setDescription("admin");
            client.setName("超级Client");
            client.setTrusted(true);

            addClientDetails(client);
        }catch (Exception e) {

        }

    }

    /**
     * Load client by client id client details.
     *
     * @param clientId the client id
     * @return the client details
     */
    @Override
    public ClientDetails loadClientByClientId(String clientId) {
        return repository.findByClientId(clientId);
    }

    /**
     * Add client details.
     *
     * @param clientDetails the client details
     */
    @Override
    public void addClientDetails(ClientDetails clientDetails) {
        repository.save((Oauth2Client) clientDetails);
    }

    /**
     * Update client details.
     *
     * @param clientDetails the client details
     */
    @Override
    public void updateClientDetails(ClientDetails clientDetails) {
        repository.save((Oauth2Client) clientDetails);
    }

    /**
     * Update client secret.
     *
     * @param clientId the client id
     * @param secret   the secret
     */
    @Override
    public void updateClientSecret(String clientId, String secret) {
        Oauth2Client oauth2Client = repository.findByClientId(clientId);
        oauth2Client.setClientSecret(secret);
        repository.save(oauth2Client);
    }

    /**
     * Remove client details.
     *
     * @param clientId the client id
     */
    @Override
    public void removeClientDetails(String clientId) {
        repository.deleteByClientId(clientId);
    }

    /**
     * List client details list.
     *
     * @return the list
     */
    @Override
    public List<ClientDetails> listClientDetails() {
        return new ArrayList<>(repository.findAll());
    }
}
